package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import com.javaweb.service.dto.UserConditionDto;
import com.javaweb.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: huai_nian
 * @date: 2024/10/16 04:00
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao ;

    @Override
    public User queryByName(String username) {

        return userDao.selectByName(username);
    }

    @Override
    public int register(User user) {
        return userDao.insert(user);
    }

    @Override
    @Transactional
    public PageResult<User> queryUsers(UserConditionDto userCondition, Integer pageNum, Integer pageSize) {

        PageResult<User> userPageResult = new PageResult<>();
        userPageResult.setData(userDao.selectUsers(userCondition,pageNum,pageSize));
        userPageResult.setPageNum(pageNum);
        userPageResult.setPageSize(pageSize);
        userPageResult.setTotal(userDao.selectCountAll());
        return userPageResult;
    }

    @Override
    @Transactional
    public void addAssignRole(Integer userId, List<Integer> roleIds) {

        for (Integer roleId:roleIds) {

            userDao.insertUserRole(userId,roleId);

        }



    }

}
